package com.example.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeaway.entity.Dish;
import com.example.takeaway.entity.DishDto;
import com.example.takeaway.entity.DishFlavor;
import com.example.takeaway.mapper.DishMapper;
import com.example.takeaway.service.DishFlavorService;
import com.example.takeaway.service.DishService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {



    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品 同时保存对应的口味数据
     * @param dishDto
     */


    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //菜品id
        Long id = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavorList = dishDto.getFlavors();
        flavorList = flavorList.stream().map((item) -> {
           item.setDishId(id);
           return item;
        }).collect(Collectors.toList());


        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavorList);




    }

    /**
     * 根据id查询商品信息和对应的口味信息
     * @param id
     * @return
     */

    @Override
    public DishDto GetByIdWithFlavor(Long id) {


        DishDto dishDto = new DishDto();


        //查询菜品基本信息
        Dish dish = this.getById(id);

        BeanUtils.copyProperties(dish,dishDto);


        //查询当前菜品的口味信息 从dish_flavor
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId , dish.getId());

        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);


        return dishDto;
    }


    // 更新菜品信息 更新 口味信息
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表基本信息
        this.updateById(dishDto);

        //获取当前菜品的id值
        Long id = dishDto.getId();


        //更新口味表基本信息  ： 1  . 清理当前菜品的口味数据 --- dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);


        dishFlavorService.remove(queryWrapper);

        // 添加当前提交过来的口味数据 -- dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());



        dishFlavorService.saveBatch(flavors);


    }

    @Transactional
    @Override
    public void DeleteByIds(List<Long> ids) {


        for(Long id : ids) {


            //删除菜品信息
            this.removeById(id);

            //删除对应的口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId , id);

            dishFlavorService.remove(queryWrapper);


        }




    }




}
